package com.pansoft.openplanet.adapter;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.efounder.util.ToastUtil;
import com.efounder.utils.ResStringUtil;
import com.efounder.view.CircleImageView;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.activity.AssetListActivity;
import com.pansoft.openplanet.activity.IPAssetsWalletDetailActivity;
import com.pansoft.openplanet.activity.NewTransferActivity;
import com.pansoft.openplanet.activity.TradeLineActivity;
import com.pansoft.openplanet.bean.Account;
import com.pansoft.openplanet.bean.AssetListBean;
import com.pansoft.openplanet.constant.IPAssetsConstant;
import com.pansoft.openplanet.db.AccountDao;
import com.utilcode.util.ToastUtils;

import java.util.ArrayList;

/**
 * ACT子钱包列表adapter
 *
 * @author YQS
 */
public class ChildACTWalletListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_HEADER = 0;
    private static final int VIEW_TYPE_BODY = 1;
    private static final int VIEW_TYPE_FOOTER = 2;

    private Context mContext;
    private ArrayList<AssetListBean> orderList;
    private int mode;
    //top 显示余额
    private String balance;

    public ChildACTWalletListAdapter(Context mContext, ArrayList<AssetListBean> orderList, int mode) {
        this.mContext = mContext;
        this.orderList = orderList;
        this.mode = mode;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == VIEW_TYPE_HEADER) {
            view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_item_wallet_header, parent, false);
            return new WalletHeaderHolder(view);
        } else if (viewType == VIEW_TYPE_BODY) {
            view = LayoutInflater.from(mContext).inflate(R.layout.open_planet_item_wallet_asset, parent, false);
            return new WalletAssetHolder(view);
        } else if (viewType == VIEW_TYPE_FOOTER) {
//            view = LayoutInflater.from(mContext).inflate(R.layout.layout_order_item_footer, parent, false);
//            return new OrderFooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof WalletHeaderHolder) {
            //头部view 以及数据
            AccountDao accountDao = new AccountDao(mContext);
            Account account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.ACHAIN);
            if (balance == null) {
                balance = "0";
            }
            balance = balance.replace(" ", "");
            ((WalletHeaderHolder) holder).tvNum.setText(balance);
            ((WalletHeaderHolder) holder).ivWalletStatus.setImageDrawable(mContext.getResources()
                    .getDrawable(R.drawable.active_wallet));
            ((WalletHeaderHolder) holder).tvTitle.setText(account.getAcccountName());
            ((WalletHeaderHolder) holder).ivAddAsset.setVisibility(View.INVISIBLE);
            ((WalletHeaderHolder) holder).ivCoin.setImageDrawable(mContext.getResources()
                    .getDrawable(R.drawable.ipasset_blue_act));
            if (account.getAccountAvatar() == null || account.getAccountAvatar().equals("")) {
                LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                        R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
            } else {
                LXGlideImageLoader.getInstance().displayRoundCornerImage(mContext, ((WalletHeaderHolder) holder).ivWallet,
                        account.getAccountAvatar(), R.drawable.star_asset_default_wallet_icon, R.drawable.star_asset_default_wallet_icon, 20);
            }


            final String walletName = account.getAcccountName();
            final String address = account.getAddress();
            final String walletIcon = account.getAccountAvatar();

            //钱包类型
            final int accountType = account.getAccountType();
            //更新钱包地址或者头像
            final Account finalAccount = account;
            ((WalletHeaderHolder) holder).tvTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startUpdateAvatar(finalAccount);
                }
            });
            ((WalletHeaderHolder) holder).ivWallet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startUpdateAvatar(finalAccount);
                }
            });

            ((WalletHeaderHolder) holder).tvList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //进入交易明细界面
                    TradeLineActivity.startTradeLineActivity
                            (mContext, mode, walletName, address, walletIcon, balance,
                                    null, null, accountType);
                }
            });
            ((WalletHeaderHolder) holder).tvOut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (accountType == Account.ACCOUNT_TYPE_OBSERVE) {
                        ToastUtil.showToast(mContext, ResStringUtil.getString(R.string.open_planet_asset_child_wallet_list_not_transfer));
                        return;
                    }
                    //普通转账，非Token代币转账
                    NewTransferActivity.start(mContext, walletName, mode, address, balance,
                            null, accountType);
                }
            });
            ((WalletHeaderHolder) holder).tvIn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                            BarcodeReceiveActivity.start(mContext, walletName, address, walletIcon,
//                                    "ETH", "", mode, "");


                }
            });
            if (address.length() > 20) {
                String formatAddress = address.replace(address.substring(6, address.length() - 4), "****");
                ((WalletHeaderHolder) holder).tvAddress.setText(formatAddress);
            } else {
                ((WalletHeaderHolder) holder).tvAddress.setText(address);
            }
            ((WalletHeaderHolder) holder).tvAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    copyAddress(address);
                }
            });
            ((WalletHeaderHolder) holder).ivCopy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    copyAddress(address);
                }
            });


            //点击进入代币列表界面，传入ETH钱包余额，作为第一条展示
            final Account finalAccount1 = account;
            ((WalletHeaderHolder) holder).ivAddAsset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, AssetListActivity.class);
                    intent.putExtra("asset_list", orderList);
                    intent.putExtra("accountAddress", finalAccount1.getAddress());
                    if (finalAccount1 != null) {
                        mContext.startActivity(intent);
                    }

                }
            });
        } else if (holder instanceof WalletAssetHolder) {
            //代币列表view以及数据
            AccountDao accountDao = new AccountDao(mContext);
            //查询当前显示的账户
            Account account = accountDao.getCurrentShowAccount(IPAssetsConstant.ChainEnum.ACHAIN);

            if (account == null) {
                account = accountDao.getMainAccount();
            }
            final String walletAddress = account.getAddress();
            final String walletName = account.getAcccountName();
            final String walletIcon = account.getAccountAvatar();
            final int accountType = account.getAccountType();
            //第一条是默认的，为ACT钱包
            if (position == 1) {

                ((WalletAssetHolder) holder).tvAssetName.setText("ACT");
                ((WalletAssetHolder) holder).tvAssetValue.setText("0");
                LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                        R.drawable.ipasset_wallet_list_act, 100);
                ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TradeLineActivity.startTradeLineActivity(mContext, mode,
                                walletName, walletAddress, walletIcon, balance,
                                null, null, accountType);
                    }
                });

            } else {
                //代币列表数据
                ((WalletAssetHolder) holder).tvAssetName.setText((orderList.get(position).getName()));
                ((WalletAssetHolder) holder).tvAssetValue.setText((orderList.get(position)).getBalance());
//            ((WalletAssetHolder) holder).tvAssetExchangeValue.setText((orderList.get(position-1)).getExchangeValue());
                LXGlideImageLoader.getInstance().showRoundUserAvatar(mContext, ((WalletAssetHolder) holder).ivWallet,
                        orderList.get(position).getIcon(), 100);
                //点击进入代币详情
                ((WalletAssetHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TradeLineActivity.startTradeLineActivity(mContext, mode, walletName,
                                walletAddress, walletIcon, balance, orderList.get(holder.getAdapterPosition()),
                                null, accountType);
                    }
                });
            }
        }

    }

    //修改钱包头像或者名称
    private void startUpdateAvatar(Account finalAccount) {
        IPAssetsWalletDetailActivity.start(mContext, finalAccount.getAddress());
    }

    @Override
    public int getItemViewType(int position) {
        if (orderList.get(position).getType() == 1) {
            return VIEW_TYPE_HEADER;
        } else {
            return VIEW_TYPE_BODY;
        }

//        if (position == 0) {
//            return VIEW_TYPE_HEADER;
//        } else {
//            return VIEW_TYPE_BODY;
//        }
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    class WalletHeaderHolder extends RecyclerView.ViewHolder {
        ConstraintLayout clHeader;
        TextView tvTitle, tvAddress, tvNum, tvUnit, tvOut, tvList, tvIn;
        ImageView ivWallet, ivCopy, ivAddAsset, ivCoin, ivWalletStatus;
        Button btnOneClickSend;

        public WalletHeaderHolder(View itemView) {
            super(itemView);
            clHeader = itemView.findViewById(R.id.cl_header);
            tvTitle = itemView.findViewById(R.id.tv_account_name);
            tvAddress = itemView.findViewById(R.id.tv_wallet_address);
            tvUnit = itemView.findViewById(R.id.tv_assets_unit);
            tvNum = itemView.findViewById(R.id.tv_assets);
            ivWallet = itemView.findViewById(R.id.iv_wallet);
            ivCopy = itemView.findViewById(R.id.iv_copy);
            ivAddAsset = itemView.findViewById(R.id.iv_add_asset);
            ivCoin = itemView.findViewById(R.id.iv_coin);
            tvOut = itemView.findViewById(R.id.tv_trade_out);
            tvIn = itemView.findViewById(R.id.tv_trade_in);
            tvList = itemView.findViewById(R.id.tv_trade_list);
            ivWalletStatus = itemView.findViewById(R.id.iv_wallet_status);
            btnOneClickSend = itemView.findViewById(R.id.btn_one_click_send);
        }
    }

    class WalletAssetHolder extends RecyclerView.ViewHolder {
        TextView tvAssetName, tvAssetValue, tvAssetExchangeValue;
        CircleImageView ivWallet;

        public WalletAssetHolder(View itemView) {
            super(itemView);
            tvAssetName = itemView.findViewById(R.id.tv_asset_name);
            tvAssetValue = itemView.findViewById(R.id.tv_asset_value);
//            tvAssetExchangeValue = itemView.findViewById(R.id.tv_exchange_value);
            ivWallet = itemView.findViewById(R.id.iv_wallet);
        }
    }

    class WalletFooterHolder extends RecyclerView.ViewHolder {
        //        TextView tvTotalNum, tvTotalPrice;
//        TextView tvRebuy, tvTrace, tvConfirm, tvCheckout;
        public WalletFooterHolder(View itemView) {
            super(itemView);
//            tvTotalNum = itemView.findViewById(R.id.tv_total_num);
//            tvTotalPrice = itemView.findViewById(R.id.tv_total);
//            tvRebuy = itemView.findViewById(R.id.tv_rebuy);
//            tvTrace = itemView.findViewById(R.id.tv_trace);
//            tvConfirm = itemView.findViewById(R.id.tv_confirm);
//            tvCheckout = itemView.findViewById(R.id.tv_checkout);
        }
    }

    /**
     * 复制地址
     *
     * @param address 地址
     */
    private void copyAddress(String address) {
        //复制账号
        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(address);
        ToastUtils.setBgColor(mContext.getResources().getColor(R.color.black));
        ToastUtils.setMsgColor(mContext.getResources().getColor(R.color.op_text_white_color));
        ToastUtils.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 150);
        ToastUtils.showShort(R.string.chat_copy_to_clipboard);
    }

    public void setData(String balance) {
        this.balance = balance;
        this.notifyItemChanged(0);
        this.notifyItemChanged(1);
    }
}
